package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.ManageMcService;
import cc.rengu.igas.momp.facade.request.*;
import cc.rengu.igas.momp.facade.response.*;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 管理营销活动接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/24 11:57
 */
@SuppressWarnings("unchecked")
public class ManageMcServiceImpl implements ManageMcService {
    @Override
    public Result<ManageAgentProfitSharingMcResponse> manageAgentProfitSharingMc(ManageAgentProfitSharingMcRequest manageAgentProfitSharingMcRequest) {

        DubboService<ManageAgentProfitSharingMcRequest> dubboService = new DubboServiceImpl();
        Result<ManageAgentProfitSharingMcResponse> manageAgentProfitSharingMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MANAGE_APS_MC_INFO.getTxnNum().equals(manageAgentProfitSharingMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ManageAgentProfitSharingMcResponse> dubboResult = dubboService.callAtomService(manageAgentProfitSharingMcRequest,
                    MompTxnNumEnum.MANAGE_APS_MC_INFO.getService(), ManageAgentProfitSharingMcResponse.class);
            manageAgentProfitSharingMcResponseResult.setResult(dubboResult.getResult());
            manageAgentProfitSharingMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ManageAgentProfitSharingMcResponse manageAgentProfitSharingMcResponse = new ManageAgentProfitSharingMcResponse();
            manageAgentProfitSharingMcResponseResult.setResult(manageAgentProfitSharingMcResponse);
            manageAgentProfitSharingMcResponse.setHeader(manageAgentProfitSharingMcRequest.getHeader());
            manageAgentProfitSharingMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                manageAgentProfitSharingMcResponse.setRespCode(((BizException) e).getCode());
                manageAgentProfitSharingMcResponse.setRespDesc(e.getMessage());
            } else {
                manageAgentProfitSharingMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                manageAgentProfitSharingMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return manageAgentProfitSharingMcResponseResult;
    }

    @Override
    public Result<ManageCashierCashbackMcResponse> manageCashierCashbackMc(ManageCashierCashbackMcRequest manageCashierCashbackMcRequest) {

        DubboService<ManageCashierCashbackMcRequest> dubboService = new DubboServiceImpl();
        Result<ManageCashierCashbackMcResponse> manageCashierCashbackMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MANAGE_CCB_MC_INFO.getTxnNum().equals(manageCashierCashbackMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ManageCashierCashbackMcResponse> dubboResult = dubboService.callAtomService(manageCashierCashbackMcRequest,
                    MompTxnNumEnum.MANAGE_CCB_MC_INFO.getService(), ManageCashierCashbackMcResponse.class);
            manageCashierCashbackMcResponseResult.setResult(dubboResult.getResult());
            manageCashierCashbackMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ManageCashierCashbackMcResponse manageCashierCashbackMcResponse = new ManageCashierCashbackMcResponse();
            manageCashierCashbackMcResponseResult.setResult(manageCashierCashbackMcResponse);
            manageCashierCashbackMcResponse.setHeader(manageCashierCashbackMcRequest.getHeader());
            manageCashierCashbackMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                manageCashierCashbackMcResponse.setRespCode(((BizException) e).getCode());
                manageCashierCashbackMcResponse.setRespDesc(e.getMessage());
            } else {
                manageCashierCashbackMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                manageCashierCashbackMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return manageCashierCashbackMcResponseResult;
    }

    @Override
    public Result<ManageCouponMcResponse> manageCouponMc(ManageCouponMcRequest manageCouponMcRequest) {


        DubboService<ManageCouponMcRequest> dubboService = new DubboServiceImpl();
        Result<ManageCouponMcResponse> manageCouponMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MANAGE_CP_MC_INFO.getTxnNum().equals(manageCouponMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ManageCouponMcResponse> dubboResult = dubboService.callAtomService(manageCouponMcRequest,
                    MompTxnNumEnum.MANAGE_CP_MC_INFO.getService(), ManageCouponMcResponse.class);
            manageCouponMcResponseResult.setResult(dubboResult.getResult());
            manageCouponMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ManageCouponMcResponse manageCouponMcResponse = new ManageCouponMcResponse();
            manageCouponMcResponseResult.setResult(manageCouponMcResponse);
            manageCouponMcResponse.setHeader(manageCouponMcRequest.getHeader());
            manageCouponMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                manageCouponMcResponse.setRespCode(((BizException) e).getCode());
                manageCouponMcResponse.setRespDesc(e.getMessage());
            } else {
                manageCouponMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                manageCouponMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return manageCouponMcResponseResult;

    }

    @Override
    public Result<ManageMchntFeeDiscountMcResponse> manageMchntFeeDiscountMc(ManageMchntFeeDiscountMcRequest manageMchntFeeDiscountMcRequest) {

        DubboService<ManageMchntFeeDiscountMcRequest> dubboService = new DubboServiceImpl();
        Result<ManageMchntFeeDiscountMcResponse> manageMchntFeeDiscountMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MANAGE_MFD_MC_INFO.getTxnNum().equals(manageMchntFeeDiscountMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ManageMchntFeeDiscountMcResponse> dubboResult = dubboService.callAtomService(manageMchntFeeDiscountMcRequest,
                    MompTxnNumEnum.MANAGE_MFD_MC_INFO.getService(), ManageMchntFeeDiscountMcResponse.class);
            manageMchntFeeDiscountMcResponseResult.setResult(dubboResult.getResult());
            manageMchntFeeDiscountMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ManageMchntFeeDiscountMcResponse manageMchntFeeDiscountMcResponse = new ManageMchntFeeDiscountMcResponse();
            manageMchntFeeDiscountMcResponseResult.setResult(manageMchntFeeDiscountMcResponse);
            manageMchntFeeDiscountMcResponse.setHeader(manageMchntFeeDiscountMcRequest.getHeader());
            manageMchntFeeDiscountMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                manageMchntFeeDiscountMcResponse.setRespCode(((BizException) e).getCode());
                manageMchntFeeDiscountMcResponse.setRespDesc(e.getMessage());
            } else {
                manageMchntFeeDiscountMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                manageMchntFeeDiscountMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return manageMchntFeeDiscountMcResponseResult;
    }

    @Override
    public Result<ManageMoneyOffMcResponse> manageMoneyOffMc(ManageMoneyOffMcRequest manageMoneyOffMcRequest) {

        DubboService<ManageMoneyOffMcRequest> dubboService = new DubboServiceImpl();
        Result<ManageMoneyOffMcResponse> manageMoneyOffMcResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.MANAGE_MO_MC_INFO.getTxnNum().equals(manageMoneyOffMcRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<ManageMoneyOffMcResponse> dubboResult = dubboService.callAtomService(manageMoneyOffMcRequest,
                    MompTxnNumEnum.MANAGE_MO_MC_INFO.getService(), ManageMoneyOffMcResponse.class);
            manageMoneyOffMcResponseResult.setResult(dubboResult.getResult());
            manageMoneyOffMcResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            ManageMoneyOffMcResponse manageMoneyOffMcResponse = new ManageMoneyOffMcResponse();
            manageMoneyOffMcResponseResult.setResult(manageMoneyOffMcResponse);
            manageMoneyOffMcResponse.setHeader(manageMoneyOffMcRequest.getHeader());
            manageMoneyOffMcResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                manageMoneyOffMcResponse.setRespCode(((BizException) e).getCode());
                manageMoneyOffMcResponse.setRespDesc(e.getMessage());
            } else {
                manageMoneyOffMcResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                manageMoneyOffMcResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }

        return manageMoneyOffMcResponseResult;
    }
}
